package com.dream.keigezhushou.Activity.kege.acty;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dream.keigezhushou.Activity.Fragment.MyKeFragment;
import com.dream.keigezhushou.Activity.Fragment.MySingingFragment;
import com.dream.keigezhushou.R;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 我的K歌
 * linmm
 */

public class MyKeSongActivity extends AppCompatActivity {
    @BindView(R.id.iv_return)
    ImageView ivReturn;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.viewPager)
    ViewPager mViewPager;
    @BindView(R.id.my_ke_song_tv)
    TextView keSongTv;
    @BindView(R.id.my_ke_song_img)
    ImageView keSongImg;
    @BindView(R.id.my_singing_tv)
    TextView singingTv;
    @BindView(R.id.my_singing_img)
    ImageView singingImg;


    private ArrayList<Fragment> fragmentList;
    private int currIndex;//当前页卡编号
    private int offset;//图片移动的偏移量
    private int bmpW;//横线图片宽度

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_ke_song);
        ButterKnife.bind(this);
        initView();
        InitViewPager();
    }


    //初始化控件
    public void initView() {
        tvTitle.setText("我的歌曲");

        ivReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        //我的ke歌点击事件
        keSongTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateTopIcon(0);
            }
        });

        //我的合唱
        singingTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateTopIcon(1);
            }
        });


    }


    //初始化Fragment控件
    public void InitViewPager() {

        fragmentList = new ArrayList<Fragment>();
        Fragment keFragment = new MyKeFragment();
        Fragment singingFragment = new MySingingFragment();
        fragmentList.add(keFragment);
        fragmentList.add(singingFragment);

        //给ViewPager设置适配器
        mViewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(), fragmentList));
        mViewPager.setCurrentItem(0);//设置当前显示标签页为第一页
        updateTopIcon(0);
        mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());//页面变化时的监听器

    }


    //viewpage选择状态
    public void updateTopIcon(int arg0) {
        if (arg0 == 0) {
            keSongTv.setTextColor(getResources().getColor(R.color.colorRefund));
            keSongImg.setVisibility(View.VISIBLE);
            singingTv.setTextColor(getResources().getColor(R.color.color_666666));
            singingImg.setVisibility(View.INVISIBLE);
            mViewPager.setCurrentItem(0);
        } else if (arg0 == 1) {
            keSongTv.setTextColor(getResources().getColor(R.color.color_666666));
            keSongImg.setVisibility(View.INVISIBLE);
            singingTv.setTextColor(getResources().getColor(R.color.colorRefund));
            singingImg.setVisibility(View.VISIBLE);
            mViewPager.setCurrentItem(1);
        }
    }


    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {
        private int one = offset * 2 + bmpW;//两个相邻页面的偏移量

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageSelected(int arg0) {
//             TODO Auto-generated method stub
            updateTopIcon(arg0);
            mViewPager.setCurrentItem(arg0);
            int i = currIndex + 1;
//             Toast.makeText(MainActivity.this, "您选择了第"+i+"个页卡", Toast.LENGTH_SHORT).show();

        }
    }


}


//viewpage的适配器
class MyFragmentPagerAdapter extends FragmentPagerAdapter {

    private ArrayList<Fragment> fragmentList;

    public MyFragmentPagerAdapter(FragmentManager fm, ArrayList<Fragment> list) {
        super(fm);
        this.fragmentList = list;

    }


    @Override
    public int getCount() {
        return fragmentList.size();
    }

    @Override
    public Fragment getItem(int arg0) {
        return fragmentList.get(arg0);
    }

}
